// pages/index/monitor/monitor.js
const app = getApp()
//获取 log 日志打印
const log = new app.appLogs()
//获取 http 网络请求
const http = new app.appHttp()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isFullScreen: false,
        yard_id: "",
        name:"",
        url: "",
        option1: [
            {
                text: '暂无',
                value: ''
            },
        ],
        value1: '',
        monitor: [
            {
                name: "左门",
                id: "12345",
                can_open: 0,
            },
        ],
        currentCampus: "",
        actions: [
            {name: '火警：' + '119'},
            {name: '匪警：' + '110'},
            {name: '急救：' + '120'},
        ],
        show: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const than = this
        than.CommunityChoice()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    changeFullScreen: function (e) {
        const than = this
        let livePlayerContext = wx.createLivePlayerContext("monitor");
        if (than.data.isFullScreen) {
            livePlayerContext.exitFullScreen();
            than.setData({
                "isFullScreen": false
            })
        } else {
            livePlayerContext.requestFullScreen({
                "direction": 90
            });
            than.setData({
                "isFullScreen": true
            })
        }
    },
    bindstatechange: function (e) {
        if (e.detail.code == 2004) {
            wx.hideLoading();
        }
        if (e.detail.code == -2302) {
            wx.hideLoading();
            wx.showModal({
                title: "温馨提示",
                content: "获取数据失败!",
                showCancel: false,
                success: function (res) {
                    if (res.confirm) {
                    }
                }
            });
        }
    },
    onchange1: function (e) {
        const than = this
        than.setData({
            yard_id: e.detail
        });
        than.VideoStreaming();
        than.CallThePolice();
    },
    /**
     * 视频流详情
     * @constructor
     */
    MonitoringDetails: function () {
        const than = this
        http.get("dev/monitor/detail", {
            monitor_id: than.data.currentCampus,
        }).then((res) => {
            if (res.data.code == 200) {
                than.setData({
                    url: res.data.data.stream_url,
                    name:res.data.data.address
                })
            } else {
            }
        })
    },
    /**
     * 单击监控列表中的某项
     * @param event
     */
    onClickmonitor: function (event) {
        const id = event.currentTarget.dataset.id
        const can_open = event.currentTarget.dataset.can_open
        const than = this
        if (can_open == 1) {
            //设置当前样式
            than.setData({
                currentCampus: id
            })
            than.MonitoringDetails()
        } else {
            Dialog.confirm({
                title: '暂无权限',
                message: '查看更多视频监控需要实名认证权限，是否前往实名认证？',
            })
                .then(() => {
                    // on confirm
                    wx.navigateTo({
                        url: '../../mine/room_register/room_register'
                      })
                })
                .catch(() => {
                    // on cancel
                });
        }

    },
    /**
     * 一键报警
     * @param event
     */
    onClickmpolice: function (event) {
        const than = this
        if (than.data.monitor != null) {
            than.setData({
                show: true
            })
        } else {
            Dialog.confirm({
                title: '提示',
                message: '当前院落暂未开放视频监控功能，敬请期待！',
            })
        }
    },
    /**
     * 一键报警取消
     * @param event
     */
    oncancelpolice: function (event) {
        const than = this
        than.setData({
            show: false
        })
    },
    /**
     * 一键报警点击项
     * @param event
     */
    onselectpolice: function (event) {
        const than = this
        const name = event.detail.name
        than.setData({
            show: false
        })
        Dialog.confirm({
            title: '拨打电话',
            message: '是否拨打' + name + '的电话？',
        })
            .then(() => {
                wx.makePhoneCall({
                    phoneNumber: name
                })
            })
            .catch(() => {
                // on cancel
            });
    },
    /**
     * 一键报警点击遮罩层
     * @param event
     */
    onoverlaypolice: function (event) {
        const than = this
        than.setData({
            show: false
        })
    },
    /**
     * 一键报警电话列表
     * 徐、余
     * @param event
     * @constructor
     */
    CallThePolice: function (event) {
        const than = this
        let CallArray = []
        http.get("phone/warn", {
            area_code: app.globalData.areaCode,
            yard_id: than.data.yard_id,
        }).then((res) => {
            if (res.data.code == 200) {
                if (res.data.data != "") {
                    res.data.data.forEach(function (item, index) {
                        CallArray.push({
                            name: item.name + "：(" + item.phone + ")",
                        })
                    })
                    than.setData({
                        actions: CallArray,
                    });
                } else {
                    than.setData({
                        actions: null,
                    });
                }
            } else {
            }
        })
    },
    /**
     * 选择社区列表
     * 徐、余
     * @param event
     * @constructor
     */
    CommunityChoice: function (event) {
        const than = this
        let Array = []
        Array.push({
            text: app.globalData.appTitle,
            value: "",
        })
        http.get("yard/select/area_code", {
            area_code: app.globalData.areaCode,
        }).then((res) => {
            if (res.data.code == 200) {
                res.data.data.forEach(function (item, index) {
                    Array.push({
                        text: item.yard_name,
                        value: item.yard_id,
                    })
                })
                than.setData({
                    option1: Array,
                    value1:  Array[0].value,
                    yard_id: Array[0].value
                });
                than.VideoStreaming();
                than.CallThePolice()
            } else {
            }
        })
    },
    /**
     * 视频监控流地址列表
     * 徐、余
     * @param event
     * @constructor
     */
    VideoStreaming: function (event) {
        const than = this
        http.get("dev/monitor/list", {
            area_code: app.globalData.areaCode,
            yard_id: than.data.yard_id,
            user_id: app.globalData.userInfo.applet_user_id,
        }).then((res) => {
            if (res.data.code == 200) {
                if (res.data.data != "") {
                    than.setData({
                        monitor: res.data.data,
                        currentCampus: res.data.data[0].monitor_id
                    })
                    than.MonitoringDetails()
                    than.CallThePolice()
                } else {
                    than.setData({
                        monitor: null,
                        url: ""
                    })
                }
            } else {
            }
        })
    }
})